import Head from "next/head";
import Image from "next/image";
import localFont from "next/font/local";
import styles from "@/styles/Home.module.css";

export default function Admin() {
  const startSSE = async () => {
    // const eventSource = new EventSource("http://localhost:30000/sse");
    // eventSource.onmessage = (event) => {
    //   console.log(event);
    // };
    const ctl = new AbortController();
    const eventSource = await fetch("http://localhost:30000/sse", {
      signal: ctl.signal,
      method: "POST",
    });

    const reader = eventSource.body?.getReader();
    while (reader) {
      const { done, value } = await reader.read();
      if (done) {
        ctl.abort();
        break;
      }

      const data = new TextDecoder().decode(value);
      console.log(JSON.parse(data.split("data: ")[1]));
    }
  };

  return (
    <>
      <div onClick={startSSE}>
        Admin
      </div>
    </>
  );
}
